package LeetCode;

import Constructs.TreeNode;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/6/1.
//
//https://leetcode.cn/problems/symmetric-tree/description/

//public boolean isSymmetric(TreeNode root) {
//        return isSymmetric(root, root);
//        }
//
//private boolean isSymmetric(TreeNode left, TreeNode right) {
//        if (left == null && right == null)
//        return true;
//
//        if (left == null || right == null)
//        return false;
//
//        if (left.val != right.val)
//        return false;
//
//        return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
//        }

public class 对称二叉树 {
    public static void main(String[] args) {
        System.out.println(isSymmetric(new TreeNode(1,new TreeNode(2,new TreeNode(2,null,null),null),new TreeNode(2,new TreeNode(2,null,null),null))));
    }
    public static boolean isSymmetric(TreeNode root) {
        if(root.left == null && root.right == null)return true;
        if(root.left == null || root.right == null)return false;
        boolean[] flag = new boolean[1];
        flag[0] = true;
        isSymmetricTree(root.left,root.right,flag);
        return flag[0];
    }

    private static void isSymmetricTree(TreeNode leftPoint, TreeNode rightPoint,boolean[] flag) {
        if(leftPoint.val != rightPoint.val){
            flag[0] = false;
            return;
        }
        if((leftPoint.left == null && rightPoint.right != null) || (leftPoint.left != null && rightPoint.right == null)){
            flag[0] = false;
            return;
        }
        if((leftPoint.right == null && rightPoint.left != null) || (leftPoint.right != null && rightPoint.left == null)){
            flag[0] = false;
            return;
        }
        if((leftPoint.left != null && rightPoint.right != null)){
            isSymmetricTree(leftPoint.left,rightPoint.right,flag);
        }
        if((leftPoint.right != null && rightPoint.left != null)){
            isSymmetricTree(leftPoint.right,rightPoint.left,flag);
        }
    }
}
